package com.stn.zjx.givingtreasure.activity.userbankcard;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.came.viewbguilib.ButtonBgUi;
import com.google.gson.Gson;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.stn.zjx.givingtreasure.R;
import com.stn.zjx.givingtreasure.adapter.BankCardItemListAdapter;
import com.stn.zjx.givingtreasure.base.BaseActivity;
import com.stn.zjx.givingtreasure.domain.BankCardDomain;
import com.stn.zjx.givingtreasure.event.BankCardEvent;
import com.stn.zjx.givingtreasure.event.NetWorkEvent;
import com.stn.zjx.givingtreasure.net.BaseApi;
import com.stn.zjx.givingtreasure.net.impl.UserBankCardNetImpl;
import com.stn.zjx.givingtreasure.service.UserService;
import com.stn.zjx.givingtreasure.table.User;
import com.vondear.rxtools.RxActivityTool;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class UserBankCardManagerActivity extends BaseActivity {
    @BindView(R.id.back_bt)
    RelativeLayout backBt;
    @BindView(R.id.action_title)
    TextView actionTitle;
    @BindView(R.id.web_title)
    TextView webTitle;
    @BindView(R.id.action_add_bt)
    RelativeLayout actionAddBt;
    @BindView(R.id.action_refresh)
    RelativeLayout actionRefresh;
    @BindView(R.id.exit)
    RelativeLayout exit;
    @BindView(R.id.sure_bt)
    TextView sureBt;
    @BindView(R.id.skip_bt)
    TextView skipBt;
    @BindView(R.id.share_bt)
    RelativeLayout shareBt;
    @BindView(R.id.more_bt)
    RelativeLayout moreBt;
    @BindView(R.id.scan_bt)
    RelativeLayout scanBt;
    @BindView(R.id.release_bt)
    ButtonBgUi releaseBt;
    @BindView(R.id.search_iv)
    ImageView searchIv;
    @BindView(R.id.search_edit)
    EditText searchEdit;
    @BindView(R.id.search_bg)
    RelativeLayout searchBg;
    @BindView(R.id.save_bt)
    TextView saveBt;
    @BindView(R.id.rv)
    RecyclerView rv;
   public  BankCardItemListAdapter adapter;
    LinearLayoutManager lm;
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onBankCardEvent(BankCardEvent event){
        switch (event.flag){
            case  BankCardEvent.AddressManager:
                initRefresh();
                break;
        }

    }
    @Override
    public void networkStatus(NetWorkEvent netWorkEvent) {

    }

    @Override
    public void initComponent() {
         actionTitle.setText(getString(R.string.bank_card));
         lm=new LinearLayoutManager(this, OrientationHelper.VERTICAL,false){
             @Override
             public boolean canScrollVertically() {
                 return false;
             }
         };
         rv.setLayoutManager(lm);
         rv.setAdapter(adapter=new BankCardItemListAdapter(this));
        initRefresh();
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_bank_manager);
        ButterKnife.bind(this);
        initComponent();
    }

    @OnClick({R.id.back_bt, R.id.add_bank_card_bt})
    public void onViewClicked(View view) {
        Bundle bundle=new Bundle();
        switch (view.getId()) {
            case R.id.back_bt:
                finish();
                break;
            case R.id.add_bank_card_bt:
                bundle.putInt("flag",BankCardEvent.AddressManager);
                RxActivityTool.skipActivity(this,AddUserBankCardActivity.class,bundle);
                break;
        }
    }

    public void initRefresh(){
        User onlineUser=UserService.getInstance(this)
                .getOnlineLoginUser();
          if(onlineUser==null){
              return;
          }
        UserBankCardNetImpl.query_bank_card_by_user_id(onlineUser.getToken(), onlineUser.getToken() + "_query_bank_card_by_user_id", new StringCallback() {
            @Override
            public void onSuccess(Response<String> response) {
                     if(response.isSuccessful()&&!isFinishing()){
                           initBankCard(response.body());
                     }
            }

            @Override
            public void onCacheSuccess(Response<String> response) {
                super.onCacheSuccess(response);
                if(response.isSuccessful()&&!isFinishing()){
                    initBankCard(response.body());
                }
            }
        });
    }

    public void initBankCard(String body){
        BankCardDomain domain=new Gson().fromJson(body,BankCardDomain.class);
           switch (domain.getStatus()){
               case BaseApi.REQUEST_SUCCESS_CODE:
                   if(domain.getData()!=null&&domain.getData().size()!=0){
                        adapter.refreshList(domain.getData());
                   }else{
                       adapter.clearList();
                   }
                   break;
                default:
                    adapter.clearList();
                    break;
           }
    }
}
